package com.wx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.domain.Doctor;
import com.wx.service.DoctorService;
import com.wx.service.impl.DoctorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 27250
 */
public class DoctorsServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        String department = req.getParameter("department");
        int pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
        int pageSize= Integer.parseInt(req.getParameter("pageSize"));
        DoctorService doctorService=new DoctorServiceImpl();
        int count=doctorService.getCountByDep(department);
        List<Doctor> doctors=doctorService.getDoctors(department,pageIndex,pageSize);
        ObjectMapper mapper=new ObjectMapper();
        Map<String,Object> map=new HashMap<String, Object>();
        System.out.println(count);
        System.out.println(doctors);
        map.put("pageTotal",count);
        map.put("doctors",doctors);


        mapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
